package com.ztjava.core.tool;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.xiaoleilu.hutool.date.DateUnit;
import com.xiaoleilu.hutool.date.DateUtil;
import com.ztjava.core.Constant;
import com.ztjava.core.utiliy.StringUtil;

public class ViewTool {
	public static List<JSONObject> tree(String code){

		Cache cache = Redis.use();
		return cache.hget(Constant.R_TREE, code);
	}
	
	public static Long hlen(String code){
		Cache cache = Redis.use();
		if(!cache.exists(Constant.R_PRE+code)){
			return 0l;
		}
		return cache.hlen(Constant.R_PRE+code);
	}
	
	/***
	 * 获取参数
	 * 
	 * @param code
	 * @return
	 */
	public static String param(String code) {
		return param(code, "");
	}

	/***
	 * 获取参数值
	 * 
	 * @param code
	 * @param defaultValue
	 * @return
	 */
	public static String param(String code, String defaultValue) {
		Cache cache = Redis.use();
		if (cache.hexists(Constant.R_PARAM, code)) {
			return cache.hget(Constant.R_PARAM, code);
		}
		return defaultValue;
	}
	// 页面获取数据字典
	public static JSONObject dataJson(String code) {
		Cache cache = Redis.use();
		//Jfinal 3.3测试发现，顺序与实际不一致，3.2没问题
		return JSONObject.parseObject(cache.hget(Constant.R_BASEDATA, code).toString()) ;
		//return cache.hget(Constant.R_BASEDATA, code) ;
	}
	// 页面获取数据字典
	public static JSONObject data(String code) {
		Cache cache = Redis.use();
		//Jfinal 3.3测试发现，顺序与实际不一致，3.2没问题
		return cache.hget(Constant.R_BASEDATA, code) ;
		//return cache.hget(Constant.R_BASEDATA, code) ;
	}

	public static String data(String code, String value) {
		return data(code, "text", value);
	}

	public static String data(String code, Integer value) {
		return data(code, "text", value + "");
	}

	public static String data(String code, String type, Integer value) {
		return data(code, type, value + "");
	}
	
	public static List<JSONObject> multiple(String code,String ids){
		List<JSONObject> list = new ArrayList<JSONObject>();
		Cache cache = Redis.use();
		if(ids==null||ids.equals("")){
			return list;
		}
		String[] arr = ids.split(",");
		JSONObject json = cache.hget(Constant.R_BASEDATA, code);
		for(String id:arr){
			if(id==null||id.equals("")){
				continue;
			}
			list.add(json.getJSONObject(id));
		}
		return list;
	}

	public static String data(String code, String type, String value) {
		
		return data(code, type, value, "name");
	}

	public static String data(String code, String type, Integer value,
			String field) {
		return data(code, type, value + "", field);
	}

	public static String data(String code, String type, String value,String field) {
		Cache cache = Redis.use();
		//部分系统测试发现，顺序与实际不一致，故采用此方法
		//JSONObject json = JSONObject.parseObject(cache.hget(Constant.R_BASEDATA, code).toString()) ;
		JSONObject json = cache.hget(Constant.R_BASEDATA, code) ;
		String str = "";
		switch (type) {
			case "option":
				if (json != null) {
					for (String key : json.keySet()) {
						if (json.getJSONObject(key).get("enable_flag") != null&& json.getJSONObject(key).getString("enable_flag").equals("F")) {
							continue;
						}
						str = str + "<option value=\"" + key + "\"";
						if (value != null && value.equals(key)) {
							str = str + " selected=\"selected\"";
						}
						str = str + ">" + json.getJSONObject(key).get(field)+ "</option>";
					}
				}
				break;
		case "search":
			if (json != null) {
				for (String key : json.keySet()) {
					str = str + "<option value=\"" + key + "\"";
					if (value != null && value.equals(key)) {
						str = str + " selected=\"selected\"";
					}
					str = str + ">" + json.getJSONObject(key).get(field);
					if (json.getJSONObject(key).get("enable_flag") != null&& json.getJSONObject(key).getString("enable_flag").equals("F")) {
						str = str + "(停用)";
					}
					str = str +  "</option>";
				}
			}
			break;
		case "multiple":
			if (json != null) {
				for (String key : json.keySet()) {
					if (json.getJSONObject(key).get("enable_flag") != null&& json.getJSONObject(key).getString("enable_flag").equals("F")) {
						continue;
					}
					str = str + "<option value=\"" + key + "\"";
					if (value != null && value.indexOf("," + key + ",") > -1) {
						str = str + " selected=\"selected\"";
					}
					str = str + ">" + json.getJSONObject(key).get(field)+ "</option>";
				}
			}
			break;
		case "multiple2":
			if (json != null) {
				for (String key : json.keySet()) {
					if (json.getJSONObject(key).get("enable_flag") != null&& json.getJSONObject(key).getString("enable_flag").equals("F")) {
						continue;
					}
					str = str + "<option value=\"" + key + "\"";
					if (value != null &&( value.indexOf(key+",") > -1||value.indexOf(","+key) > -1||value.indexOf(","+key+",") > -1||value.equals(key))) {
						str = str + " selected=\"selected\"";
					}
					str = str + ">" + json.getJSONObject(key).get(field)
							+ "</option>";
				}
			}
			break;
		case "text":
			if (json != null) {
				if (json.get(value) != null) {
					str = json.getJSONObject(value).getString(field);
				} else {
					str = "";
				}

			} else {
				str = value;
			}
			break;
		case "texts":
			if (json != null) {
				String[] valueArr = value.split(",");
				if (valueArr != null) {
					for (int i = 0; i < valueArr.length; i++) {
						if (json.get(valueArr[i]) == null) {
							continue;
						}
						str = str+ json.getJSONObject(valueArr[i]).getString(
										field) + ",";
					}
					if (!str.equals("")) {
						str = str.substring(0, str.length() - 1);
					}
				}
			} else {
				str = value;
			}
			break;

		default:
			break;
		}
		return str;
	}
	

	public static String date(Timestamp date,String format){
		if(date==null) return "";
		return DateUtil.format(date, format);
	}
	public static String date(String date,String format){
		if(date==null||date.equals("")) return "";
		return DateUtil.format(DateUtil.parse(date), format);
	}
	/***
	 * 读取表单配置
	 * @param model
	 * @param code
	 * @return
	 */
	public static Map<String,Record> form(String model,String code){
		Cache cache = Redis.use();
		if(!cache.hexists(Constant.R_PRE+"COMMON_FORM", model)){
			return null;
		}
		Map<String,Map<String,Record>> modelMap = cache.hget(Constant.R_PRE+"COMMON_FORM", model);
		return modelMap.get(code);
	}
	public static String user(Integer id){
		return user(id,"name","text");
	}
	public static String user(Integer id,String field,String type){
		if(id==null) return "";
		return user(id.toString(), field, type);
	}
	public static String user(String id,String field,String type){
		Cache cache = Redis.use();
		String str = "";
		switch (type) {
			case "text":
				JSONObject jsonObject = cache.hget(Constant.R_USER, id);
				if(jsonObject !=null&&jsonObject.get(field)!=null){
					str = jsonObject.get(field).toString();
				}
				break;
			default:
				break;
		}
		return str;
	}
	public static String users(String ids,String field,String type){
		Cache cache = Redis.use();
		String str = "";
		if(ids==null||ids.equals("")) return str;
		String[] arr = ids.split(",");
		for(String id:arr){
			switch (type) {
				case "multiple":
					JSONObject jsonObject = cache.hget(Constant.R_USER, id);
					if(jsonObject !=null&&jsonObject.get(field)!=null){
						str = jsonObject.get(field).toString();
						str = str + "<option value=\""+id+"\" selected=\"selected\">"+ jsonObject.get(field).toString()+"</option>";
					}
					break;
				default:
					break;
			}
		}
		return str;
	}
	/***
	 * 字符串转JSONObject
	 * @param str
	 * @return
	 */
	public static JSONObject toJSON(String str){
		if(str==null){
			return null;
		}
		return JSONObject.parseObject(str);
	}

	/****
	 * 其它类的方法共享下
	 */
	/***
	 * 首字母小写
	 * 
	 * @param str
	 * @return
	 */
	public static String firstLowerCase(String str) {
		return StringUtil.firstLowerCase(str);
	}

	/***
	 * 前端获取当前时间
	 * 
	 * @return
	 */
	public static String now() {
		return DateUtil.now();
	}
	
	public static String textarea(String str){
		if(str == null) return "";
		return str.replace("\r\n", "<br/>");
	}

	public static String format(Timestamp timestamp,String format){
		return DateUtil.format(timestamp, format);
	}

	public static String dateChange(Timestamp timestamp){
		String str = "";
		if(timestamp==null) return str;
		Date now = new Date(); 
		long ms = DateUtil.betweenMs(timestamp, now)/1000;
		if(ms<60){
			str = DateUtil.between(timestamp, now, DateUnit.SECOND)+"秒前";
		}else if(ms>=60&&ms<60*60){
			str = DateUtil.between(timestamp, now, DateUnit.MINUTE)+"分钟前";
		}else if(ms>=60*60&&ms<60*60*24){
			str = DateUtil.between(timestamp, now, DateUnit.HOUR)+"小时前";
		}else if(ms>=60*60*24&&ms<60*60*24*30){
			str = DateUtil.between(timestamp, now, DateUnit.DAY)+"天前";
		}else if(ms>=60*60*24*30&&ms<60*60*24*365){
			str = DateUtil.format(timestamp, "MM-dd");
		}else if(ms>=60*60*24*365){
			str = DateUtil.format(timestamp, "yyyy-MM-dd");
		}
		return str;
	}
	public static Long betweenDay(Timestamp timestamp){
		if(timestamp==null) return null;
		Date now = new Date(); 
		return DateUtil.betweenDay(timestamp, now, true);
	}
	
	public static String hiddenPhone(String phone){
		if(phone==null||phone.length()<11){
			return "";
		}
		return phone.substring(0,3)+"****"+phone.substring(7,11);
	}
}
